﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security;
using System.Collections;
using System.Data.SqlClient;
using System.Xml.Linq;

namespace GUI
{
    public partial class FormCSDL : Form
    {
        public string connection;
        public FormCSDL()
        {
            InitializeComponent();
        }

        private void cbbMayChu_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
               

        private void FormCSDL_Load(object sender, EventArgs e)
        {
            //button3_Click(null,null);
        }
        private SqlConnection TestKetNoi(String s)        {
            
            //MessageBox.Show(connection);
            SqlConnection conn =new SqlConnection(s);
            return conn;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            ArrayList networkComputers = GUI.NetworkBrowser.getNetworkComputers();
            this.cbbMayChu.DataSource = networkComputers;
            //cbbMayChu.SelectedIndex = 0;            
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            connection = "Data Source=" + this.cbbMayChu.Text + ";Persist Security Info=True;User ID=" + this.txtUser.Text + ";Password=" + this.txtPW.Text;
            SqlConnection conn = TestKetNoi(connection);
            try
            {
                    conn.Open();               
                    SqlDataAdapter dt = new SqlDataAdapter("SELECT name FROM dbo.sysdatabases", conn);
                    DataTable tb = new DataTable();
                    MessageBox.Show("Kết nối thành công!","Kết nối", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dt.Fill(tb);
                    cbbCSDL.DataSource = tb;
                    cbbCSDL.ValueMember = "name";                   
            }
            catch (Exception ex)
            {
                MessageBox.Show("Kết nối thất bại!\n" + ex.Message, "Kết nối", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally 
            {
                conn.Close();
            }
        }

        private void btnKetNoi_Click(object sender, EventArgs e)
        {
            //String a = "Data Source=.;Initial Catalog=QLLVT;Persist Security Info=True;User ID=sa;Password=123456";
            //String b = "data\\ConnectionString.xml";
            //WriterToXml(b,connection);            
            connection = "Data Source=" + this.cbbMayChu.Text + ";Initial Catalog="+this.cbbCSDL.Text+";Persist Security Info=True;User ID=" + this.txtUser.Text + ";Password=" + this.txtPW.Text;
            //MessageBox.Show(connection);
            SqlConnection conn = TestKetNoi(connection);
            try
            {                
                String sql = "SELECT TTHIENTAI FROM LICHDI";
                SqlCommand cmd = new SqlCommand(sql,conn);                
                MessageBox.Show("Kết nối thành công!", "Kết nối", MessageBoxButtons.OK, MessageBoxIcon.Information);               
                String b = "data\\ConnectionString.xml";               
                WriterToXml(b, connection);
                this.Close();
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Kết nối thất bại!\n" + ex.Message, "Kết nối", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                conn.Close();
            }
        }
        private void WriterToXml(String FileName,String data)
        {
            XDocument xmlDoc = XDocument.Load(FileName);
            xmlDoc.Element("connectionString").ReplaceAll(data);            
            xmlDoc.Save(FileName);
        }      
    }
}


